﻿package
{
	import flash.display.Sprite;  
	import flash.display.MovieClip;  
	import flash.display.Loader;  
	import flash.events.Event;  
	import flash.filters.BitmapFilter;  
	import flash.filters.DropShadowFilter;  
	import flash.text.TextFormat;  
	import flash.text.TextField;  
	import flash.text.AntiAliasType;  
	import flash.events.MouseEvent;  
	import fl.transitions.Tween;  
	import fl.transitions.easing.*;  
	import fl.transitions.TweenEvent;  
	import flash.display.MovieClip;
	
 
	
	public class MainButton extends MovieClip
	{		
		var widthTween:Tween;
		var alphaTween:Tween;
		var colorTween:Tween;
		var xTween:Tween;
		var buttonName:String;
		var isClicked:Boolean;		
		var contentToLoad:String;		
		var screenmask:Sprite;
		var greyscreen:Sprite;
		var bluescreen:Sprite;
		var h:int = 35;
		var w:int = 200;
		var buttonText:TextField = new TextField();	
		
		var hasSub:Boolean;
		var hasContent:Boolean;
		
		public function MainButton(b:String, format:TextFormat, s:Boolean, c:Boolean, ctl:String=""  ):void
		{
			buttonName = b;
			contentToLoad = ctl;
			hasSub = s;
			hasContent = c;
									
			addChild(buttonText);
			buttonText.x = 0;
			buttonText.height = h;
			buttonText.width = w;
			buttonText.text = buttonName;
			buttonText.selectable = false;
			buttonText.setTextFormat(format);
			buttonText.antiAliasType = "advanced";
			buttonText.embedFonts = true;
			
			//font size to pixel conversion
			var pixels:int = ((int)(format.size) * 4) / 3;
			buttonText.y = (h - pixels) / 2;			
			
			screenmask = new Sprite();
			screenmask.graphics.beginFill(0x333333, 0);
			screenmask.graphics.drawRect(0,0,w,h);
			screenmask.graphics.endFill();				
			screenmask.width = 0;		
			
			greyscreen = new Sprite();
			greyscreen.graphics.beginFill(0x333333, .5);
			greyscreen.graphics.drawRect(0,0,w,h);
			greyscreen.graphics.endFill();			
			
			bluescreen = new Sprite();
			bluescreen.graphics.beginFill(0x000099, .4)
			bluescreen.graphics.drawRect(0,0,w,h);
			bluescreen.graphics.endFill();
			bluescreen.alpha = 0;
			
			this.addChildAt(bluescreen, 0);
			this.addChildAt(greyscreen, 1);		
			this.addChildAt(screenmask, 2);	
			
			bluescreen.mask = screenmask;
			greyscreen.mask = screenmask;
			
			isClicked = false;	
		}
		public function addListeners():void
		{
			this.addEventListener(MouseEvent.ROLL_OVER, rollOverTween );
			this.addEventListener(MouseEvent.ROLL_OUT, rollOutTween );
			this.addEventListener(MouseEvent.CLICK, acolorTween);			
		}
		public function removeListeners():void
		{
			this.removeEventListener(MouseEvent.ROLL_OVER, rollOverTween );
			this.removeEventListener(MouseEvent.ROLL_OUT, rollOutTween );
			this.removeEventListener(MouseEvent.CLICK, acolorTween);			
		}
		public function resetColorTween():void
		{
			this.addEventListener(MouseEvent.ROLL_OVER, rollOverTween );
			this.addEventListener(MouseEvent.ROLL_OUT, rollOutTween );
			
			if (isClicked)
			{	
				alphaTween = new Tween(bluescreen, "alpha", Regular.easeOut, 1, 0, .1 , true);
				widthTween = new Tween(screenmask, "width", Regular.easeOut, screenmask.width, 0, .2, true);
			}
			else
			{
			}
			isClicked = false;
		}
		public function acolorTween(e:Event):void
		{			
			alphaTween = new Tween(bluescreen, "alpha", Regular.easeInOut,0, 1,.5,true);			
			this.removeEventListener(MouseEvent.ROLL_OVER, rollOverTween );
			this.removeEventListener(MouseEvent.ROLL_OUT, rollOutTween );
			isClicked = true;
		}
		public function rollOverTween(e:Event):void
		{
			widthTween = new Tween(screenmask, "width", Regular.easeInOut, screenmask.width, w, .4, true);
			alphaTween = new Tween(greyscreen, "alpha", Regular.easeInOut, greyscreen.alpha, 1, .4,true);
		}
		public function rollOutTween(e:Event):void
		{
			widthTween = new Tween(screenmask, "width", Regular.easeOut,screenmask.width, 0, .6, true);
			alphaTween = new Tween(greyscreen, "alpha", Regular.easeOut, greyscreen.alpha, .5,.6,true);
		}
	}
}